<?php

/**
 * Description of User
 *
 * @author Administrator
 */
class User extends CI_Controller {

  protected $class;
  protected $model;

  public function __construct() {
    parent::__construct();
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $this->class = $this->router->fetch_class();
    $model_name = $this->class . "_model";
    $this->model = new $model_name();
    set_userdata('currentURL', current_url());
  }

  protected function _list_data() {
    return array('groups' => $this->group_model->get_all());
  }

  public function index($max = '', $offset = '') {
    $offset = $offset + 0;
    $max = $this->setting_model->item('max_record');
    $total = $this->model->count();
    $this->load->helper('paging');
    $paging = paging('admin/'. $this->class . '/index', $total, $max, $offset);
    $this->load->view('admin/' . $this->class . '/list', array_merge($this->_list_data(), array(
                'objects' => $this->model->get_all(null, null, null, null, null, null, null, null, 
                        null, null, null, null, array('created_on' => 'desc'), $max, $offset),
                'total' => $total, 'offset' => $offset, 'max' => $max, 'paging' => $paging)));
  }

  public function show($id = '') {
    $obj = $this->model->get($id);
    if (!empty($obj))
      $this->load->view('admin/' . $this->class . '/show', array('obj' => $obj));
    else {
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
      redirect('admin/'.$this->class);
    }
  }

  public function create() {
    $this->load->view('admin/' . $this->class . '/create');
  }

  public function save() {
    $user = $this->user_model->create_obj($_REQUEST);
    $errors = $this->user_model->validate($user);
    if (!empty($errors))
      set_flash_error($errors);
    else {
      $user['salt'] = $this->auth->generateActiveCode(10);
      $user['password'] = $user['confirm_pass'] = md5($user['salt'] . md5($user['password']));
      $id = $this->model->save($_REQUEST);
      if (is_array($id))
        set_flash_error($id);
      else
      if (!empty($id)) {
        set_flash_message($this->lang->line('message.created', array($this->lang->line('label.' . $this->class), $id)));
        redirect('admin/'.$this->class);
        return;
      } else
        set_flash_error($this->lang->line('error.faltal.error'));
      $this->load->view('admin/' . $this->class . '/create', array('obj' => $_REQUEST));
    }
  }

  public function edit($id = '') {
    $obj = $this->model->get($id);
    if (!empty($obj))
      $this->load->view('admin/' . $this->class . '/edit', array('obj' => $obj));
    else {
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
      redirect('admin/'.$this->class);
    }
  }

  public function update() {
    $user = $this->user_model->create_obj($_REQUEST);
    $errors = $this->user_model->validate($user);
    if (!empty($errors))
      set_flash_error($errors);
    else {
      if (!empty($_REQUEST['password'])) {
        $user['password'] = $user['confirm_pass'] = md5($user['salt'] . md5($_REQUEST['password']));
      } else unset($user['password']);
      $id = $this->model->save($user);
      if (is_array($id))
        set_flash_error($id);
      else
      if ($id === true) {
        set_flash_message($this->lang->line('message.updated', array($this->lang->line('label.' . $this->class), $_REQUEST['id'])));
        redirect('admin/'.$this->class);
        return;
      } else
        set_flash_error($this->lang->line('error.faltal.error'));      
    }
	$this->load->view('admin/' . $this->class . '/edit', array('obj' => $_REQUEST));
  }

  public function delete($id='') {
    $obj = $this->model->get($id);
    if (!empty($obj)) {
      $this->end_user_model->delete(array('user_id' => $id));
      $this->provider_user_model->delete(array('user_id' => $id));
      $this->model->delete($id);
      set_flash_message($this->lang->line('message.deleted', array($this->lang->line('label.' . $this->class), $id)));
    } else
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
    redirect('admin/'.$this->class);
  }

  public function action() {
    if (!empty($_REQUEST['btnAction']) && !empty($_REQUEST['action_to'])) {
      $action_to = $_REQUEST['action_to'];
      $action_to = is_array($action_to) ? $action_to : array($action_to);
      if ($_REQUEST['btnAction'] == 'delete') {
        foreach ($action_to as $id) {
          $this->end_user_model->delete(array('user_id' => $id));
          $this->provider_user_model->delete(array('user_id' => $id));
          $this->model->delete(array('id' => $id));
        }
      } else if ($_REQUEST['btnAction'] == 'activate') {
        foreach ($action_to as $id) {
          $this->db->where('id', $id);
          $this->db->update(DB_TABLE_USER, array('is_active' => true));
        }
      } else if ($_REQUEST['btnAction'] == 'deactivate') {
        foreach ($action_to as $id) {
          $this->db->where('id', $id);
          $this->db->update(DB_TABLE_USER, array('is_active' => false));
        }
      }
      set_flash_message($this->lang->line('message.action.success'));
    } else
      set_flash_error($this->lang->line('error.action.no.select'));
    redirect('admin/'.$this->class);
  }

  public function search($max = '', $offset = '') {
    $offset = $offset + 0;
    $conditions = array('is_active' => empty($_REQUEST['is_active']) ? '0' : $_REQUEST['is_active'],
        'group_id' => empty($_REQUEST['group_id']) ? '' : $_REQUEST['group_id']);
    $in_list = array();
    $params = array();
    foreach ($conditions as $field => $value) {
      if ($value == 'all') {
        unset($conditions[$field]);
        continue;
      }
      if (is_array($value)) {
        foreach ($value as $v)
          $params[] = $field . '[]=' . $v;
        $in_list[$field] = $value;
        unset($conditions[$field]);
      } else
        $params[] = $field . '=' . $value;
    }
    $params = '?'.implode('&', $params);
    $total = $this->model->count($conditions, null, $in_list);
    $max = $this->setting_model->item('max_record');
    $this->load->helper('paging');
    $paging = paging('admin/'.$this->class . '/search', $total, $max, $offset, array(), $params);

    $this->load->view('admin/' . $this->class . '/list', array_merge($_REQUEST, $this->_list_data(), array(
                'objects' => $this->model->get_all($conditions, null, 
                        $in_list, null, null, null, null, null, null, null, null, null, null, $max, $offset),
                'total' => $total, 'offset' => $offset, 'max' => $max, 'paging' => $paging)));
  }

  public function edit_profile() {
    $obj = $this->auth->get_user_login();
    if (!empty($obj)) {
      $this->load->view('admin/' . $this->class . '/profile', array('obj' => $obj));
    } else {
      set_flash_error($this->lang->line('error.profile.not.found'));
      redirect('admin/'.$this->class);
    }
  }

  public function update_profile() {
    if (userdata('id')) {
      $_REQUEST['id'] = userdata('id');
      $user = $this->user_model->create_obj($_REQUEST);
      $errors = $this->user_model->validate($user);
      if (!empty($errors))
        set_flash_error($errors);
      else {
        if (!empty($_REQUEST['password'])) {
          $user['password'] = $user['confirm_pass'] = md5($user['salt'] . md5($_REQUEST['password']));
        } else unset($user['password']);
        $id = $this->model->save($user);
        if (is_array($id))
          set_flash_error($id);
        else
        if ($id === true) {
          set_flash_message($this->lang->line('message.profile.updated'));
          redirect('admin/user/edit_profile');
          return;
        } else
          set_flash_error($this->lang->line('error.faltal.error'));
      }
      $this->load->view('admin/' . $this->class . '/profile', array('obj' => $_REQUEST));
    } else {
      redirect('admin/index');
    }
  }

}